package question.solution33;

import question.SolutionTemplate;

public class Solution33 implements SolutionTemplate {

    public int search(int[] nums, int target) {
        int length = nums.length;
        int left = 0;
        int right = length-1;
        int midIndex;
        while(left <= right && left >=0 && left <=length-1 && right >=0 && right <=length-1){
            midIndex = (left+right)/2;
            if(nums[midIndex] == target){
                return midIndex;
            }
            if(nums[midIndex] >= nums[0]){
                if(target < nums[midIndex] && target >= nums[0]){
                    right = midIndex-1;
                }else{
                    left = midIndex+1;
                }
            }else{
                if(target > nums[midIndex] && target <= nums[length-1]){
                    left = midIndex+1;
                }else{
                    right = midIndex-1;
                }
            }
        }
        return -1;
    }


    public void test() {
//        int[] fuck1 = {3,0,1,2};
//        if(search(fuck1, 3) != 0){
//            System.out.println("error");
//        }
//        int[] fuck2 = {1,0};
//        if(search(fuck2, 0) != 1){
//            System.out.println("error");
//        }
//        if(search(fuck2, 1) != 0){
//            System.out.println("error");
//        }
//        int[] fuck3 = {4,5,6,7,0,1,2};
//        if(search(fuck3, 0) != 4){
//            System.out.println("error");
//        }
        int[] fuck4 = {1,3,5};
        if(search(fuck4, 1) != 0){
            System.out.println("error");
        }
        return;
    }
}
